package com.ebt.m.customer.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.pulltorefresh.PullToRefreshView;

/**
 *
 * @author Rick.Wang
 * @date 2016/12/17
 */
public abstract class BaseLazySwipeRefreshFragment extends BaseLazyFragment{

//    protected SwipeRefreshLayout mSwipeRefreshLayout;
    protected PullToRefreshView mSwipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base_lazy_swipe, null);
        mSwipeRefreshLayout = (PullToRefreshView)view.findViewById(R.id.swipe_layout);
        initSwipeRefreshLayout(mSwipeRefreshLayout);
        onCreateContentView(initContentView(inflater, getLayoutId()), savedInstanceState);
        return view;
    }

    protected abstract int getLayoutId();
//    protected abstract void onSwipeRefresh(SwipeRefreshLayout swipeRefreshLayout);
    protected abstract void onSwipeRefresh(PullToRefreshView swipeRefreshLayout);

    protected abstract void onCreateContentView(View contentView, Bundle savedInstanceState);

    protected void hideRefresh(){
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    protected void showRefresh(){
        if (mSwipeRefreshLayout != null && !mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    private View initContentView(LayoutInflater inflater, int layoutId){
        View content = inflater.inflate(layoutId, null);
        mSwipeRefreshLayout.addView(content);
        return content;
    }

    /*protected void initSwipeRefreshLayout(final SwipeRefreshLayout swipeRefreshLayout){
        swipeRefreshLayout.setColorSchemeResources(R.color.btn_prime_color);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);;
        swipeRefreshLayout.setProgressViewEndTarget(true, 150);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    onSwipeRefresh(swipeRefreshLayout);
                }
            });
    }*/

    protected void initSwipeRefreshLayout(final PullToRefreshView swipeRefreshLayout){
//        swipeRefreshLayout.setColorSchemeResources(R.color.btn_prime_color);
//        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);;
//        swipeRefreshLayout.setProgressViewEndTarget(true, 150);
        swipeRefreshLayout.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onSwipeRefresh(swipeRefreshLayout);
            }
        });
    }

    protected void setAbsListViewTarget(AbsListView absListView){
        if (absListView == null){
            return;
        }

        mOnSwipeIsValid = new OnSwipeIsValid() {
            @Override
            public void setSwipeEnabledTrue() {
                mSwipeRefreshLayout.setEnabled(true);
            }

            @Override
            public void setSwipeEnabledFalse() {
                mSwipeRefreshLayout.setEnabled(false);
            }
        };

        absListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            private int lastItem;
            private int totalItem;
            private boolean isLoading;

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                this.lastItem = firstVisibleItem + visibleItemCount;
                this.totalItem = totalItemCount;
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (this.totalItem == lastItem && scrollState == SCROLL_STATE_IDLE) {
                    if (!isLoading) {
                        isLoading = true;
                    }
                }
                if (mOnSwipeIsValid!=null && view.getChildCount() > 0 && view.getFirstVisiblePosition() == 0
                        && view.getChildAt(0).getTop() >= view.getPaddingTop()) {
                    mOnSwipeIsValid.setSwipeEnabledTrue();
                } else {
                    mOnSwipeIsValid.setSwipeEnabledFalse();
                }
            }
        });
    }

    protected void setRecyclerViewTarget(RecyclerView recyclerView){
        if (recyclerView == null){
            return;
        }
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                if (!recyclerView.canScrollVertically(-1)) {
//                    onScrolledToTop();
//                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mOnSwipeIsValid == null){
                    return;
                }
                if (recyclerView.canScrollVertically(-1)) {
                    mOnSwipeIsValid.setSwipeEnabledFalse();
                } else {
                    mOnSwipeIsValid.setSwipeEnabledTrue();
                }

            }
        });
    }

    protected OnSwipeIsValid mOnSwipeIsValid;

    public interface OnSwipeIsValid{
        void setSwipeEnabledTrue();
        void setSwipeEnabledFalse();
    }
}